package bb.lanxing.device.sgdevcie.xpair

import androidx.fragment.app.activityViewModels
import androidx.navigation.fragment.findNavController
import bb.lanxing.R
import bb.lanxing.databinding.FragmentXpairIntroBinding
import bb.lanxing.databinding.LayoutXpairIntroItemBinding
import bb.lanxing.device.sgdevcie.xpair.viewmodel.XPairViewModel
import bb.lanxing.mvvm.base.BaseDBFragment

class XPairIntroFragment :
    BaseDBFragment<FragmentXpairIntroBinding>(R.layout.fragment_xpair_intro) {

    private val xPairViewModel by activityViewModels<XPairViewModel>()

    override fun initView(t: FragmentXpairIntroBinding) {
        xPairViewModel.xPairUIModel?.xPairIntroUIModel ?: return
        val model = xPairViewModel.xPairUIModel?.xPairIntroUIModel
        if (model != null && null != model.tips) {
            for ((index, item) in model.tips.withIndex()) {
                val inflater = LayoutXpairIntroItemBinding.inflate(layoutInflater)
                (index + 1).toString().also { inflater.tvIndex.text = it }
                inflater.tvContent.text = item
                t.llTips.addView(inflater.root)
            }
            val btn = t.tvAction
            btn.text = getString(model.actionString)
            btn.setOnClickListener {
                findNavController().navigate(R.id.action_XPairIntroFragment_to_XPairSearchFragment)
            }
            t.imgIntroPair.setImageResource(model.introPic)
        }
    }

    override fun onResume() {
        super.onResume()
        val xPairUIModel = xPairViewModel.xPairUIModel
        xPairUIModel?.let {
            sendMessage(
                R.id.msg_change_title, getString(R.string.st_pair_your_device, it.deviceName)
            )
        }
    }
}